//
//  CourseSectionHeaderView.m
//  TrainTree
//
//  Created by wanghui on 2017/9/4.
//  Copyright © 2017年 YouYeShu. All rights reserved.
//

#import "CourseSectionHeaderView.h"
#import "CourseSectionHeaderCell.h"
static NSString *ID = @"CourseSectionHeaderCell";
@interface CourseSectionHeaderView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>


/**title数组*/
@property(nonatomic,strong)NSArray *titleArray;
/**颜色数组*/
@property(nonatomic,strong)NSArray *colorArray;
/**icon数组*/
@property(nonatomic,strong)NSArray *iconArray;
/**icon选中数组*/
@property(nonatomic,strong)NSArray *iconSelectArray;


@end

@implementation CourseSectionHeaderView

- (void)awakeFromNib {
    [super awakeFromNib];

    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        //    flowLayout.itemSize = CGSizeMake(119, 40);
        //    flowLayout.minimumInteritemSpacing = -1;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    [self.collectionView setCollectionViewLayout:flowLayout];
    [self.collectionView registerNib:[UINib nibWithNibName:@"CourseSectionHeaderCell" bundle:nil] forCellWithReuseIdentifier:ID];
//    self.titleArray = @[@"通用",@"领导力",@"运营",@"人力资源",@"财务",@"精益",@"第七个",@"第八个"];
    self.iconArray = @[@"universe",@"leadership",@"operation",@"hr",@"finance",@"lec",@"universe",@"leadership",@"operation",@"hr",@"finance",@"lec"];
    self.iconSelectArray = @[@"universe_select",@"leadership_select",@"operation_select",@"hr_select",@"finance_select",@"lec_select",@"universe_select",@"leadership_select",@"operation_select",@"hr_select",@"finance_select",@"lec_select"];
    self.colorArray = @[universeColor,leaderColor,operationColor,hrColor,financeColor,lecColor,universeColor,leaderColor,operationColor,hrColor,financeColor,lecColor];
//    self.selectArray = [[NSMutableArray alloc] initWithArray: @[@"1",@"0",@"0",@"0",@"0",@"0",@"0",@"0"]];
}
#pragma mark - collectionView 代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.themeArray.count;
        //    return self.cellArray.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    return CGSizeMake(ScreenWidth / self.themeArray.count, 81.0f);
}
#pragma mark - 横向间距
- (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0.0f;
}
#pragma mark - 纵向向间距
- (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0.0f;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0,0,0,0);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    CourseSectionHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    if (self.themeArray.count > 0) {
        cell.title.text = self.themeArray[indexPath.item][@"themeName"];
    }
     [cell configColor: self.colorArray[indexPath.item]];

    if (self.selectArray.count > 0) {
        if ([self.selectArray[indexPath.item] isEqualToString: @"1"]) {
            cell.line.hidden = NO;
            [cell.icon setImage:ImageNamed(self.iconSelectArray[indexPath.item]) forState:UIControlStateNormal];
            cell.icon.backgroundColor = self.colorArray[indexPath.item];
        }else{
            cell.line.hidden = YES;
            cell.icon.backgroundColor = [UIColor whiteColor];
            [cell.icon setImage:ImageNamed(self.iconArray[indexPath.item]) forState:UIControlStateNormal];
        }
    }
 
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectArray.count > 0) {
        for (int i = 0; i < self.selectArray.count; i++) {
            if (indexPath.item == i) {
                self.selectArray[i] = @"1";
            }else{
                self.selectArray[i] = @"0";
            }
        }
    }

    [self.collectionView reloadData];

    if(_delegate && [_delegate respondsToSelector:@selector(sectionHeaderChangeUIWith:index:)]) {
            // 把类型ID传过去
        if (self.themeArray.count > 0) {
            [_delegate sectionHeaderChangeUIWith: self.themeArray[indexPath.item][@"themeId"] index:indexPath.item];
        }
    }
}

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
//    CourseSectionHeaderCell * cell = (CourseSectionHeaderCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    cell.line.hidden = YES;
//    cell.title.textColor = rgb(164.0f, 164.0f, 164.0f);
//    [cell.icon setImage:ImageNamed(self.iconArray[indexPath.item]) forState:UIControlStateNormal];
//    cell.icon.backgroundColor = [UIColor whiteColor];
//    cell.color = self.colorArray[indexPath.item];
}

 
- (NSArray *)iconArray
{
    return _iconArray ?: (_iconArray = [[NSArray alloc]init]);
}
- (NSArray *)iconSelectArray
{
    return _iconSelectArray ?: (_iconSelectArray = [[NSArray alloc]init]);
}
- (NSArray *)titleArray
{
    return _titleArray ?: (_titleArray = [[NSArray alloc]init]);
}

- (NSArray *)colorArray
{
    return _colorArray ?: (_colorArray = [[NSArray alloc]init]);
}
- (NSMutableArray *)selectArray
{
    return _selectArray ?: (_selectArray = [[NSMutableArray alloc]init]);
}
- (NSMutableArray *)themeArray
{
    return  _themeArray ?:(_themeArray = [NSMutableArray array]);
}


@end
